package com.ar4j.spring;

import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.PropertyPlaceholderHelper;

/**
 * A utility that retrieves values from an AdapterQualifier annotation instance and replaces
 * any placeholders as needed.
 */
public class AdapterQualifierAccessor {
  private static final String DEFAULT_VALUE_SEPARATOR = ":";
  
  private PropertyPlaceholderHelper helper;
  private Properties placeholderValues;
    
  /**
   * Construct a new instance with the given prefix/suffix values and a set of properties that can be
   * used to retrieve placeholder values.
   */
  public AdapterQualifierAccessor(String placeholderPrefix, String placeholderSuffix, Properties placeholderValues) {
    this.helper = new PropertyPlaceholderHelper(placeholderPrefix, placeholderSuffix, DEFAULT_VALUE_SEPARATOR, false);
    this.placeholderValues = placeholderValues;
  }
  
  /**
   * @return the springQualifier annotation value with any placeholders replaced by real values
   */
  public String getSpringQualifier(AdapterQualifier qualifier) {
    String out = null;
    if(qualifier != null && StringUtils.isNotBlank(qualifier.springQualifier())) {
      out = helper.replacePlaceholders(qualifier.springQualifier(), placeholderValues);
    }
  
    return out;
  }

  /**
   * @return the schemaName annotation value with any placeholders replaced by real values
   */
  public String getSchemaName(AdapterQualifier qualifier) {
    String out = null;
    if(qualifier != null && StringUtils.isNotBlank(qualifier.schemaName())) {
      out = helper.replacePlaceholders(qualifier.schemaName(), placeholderValues);
    }
  
    return out;
  }
  
}